package com.rt.easyjava.base.modules.timerjob.service.job.jobStrategy.strategy;

import com.alibaba.fastjson.JSON;
import com.rt.tmpt.service.utils.SpringContextHolder;
import com.rt.easyjava.base.modules.timerjob.dto.TmptTimerBizDto;
import com.rt.easyjava.base.modules.timerjob.service.job.jobStrategy.JobStrategy;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.StringUtils;
import com.rt.tmpt.service.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Spring实例
 */
@Slf4j
public class StringBeanJobStrategy extends JobStrategy {

    public StringBeanJobStrategy(TmptTimerBizDto tmptTimerBizDto,Result typeResult) {
        super.tmptTimerBizDto = tmptTimerBizDto;
        super.typeResult = typeResult;
    }

    @Override
    public String job() throws Exception {
        String className = tmptTimerBizDto.getClassName();
        String param = tmptTimerBizDto.getParam();

        String classInstance = (String) typeResult.getMap().get("classInstance");
        String classMethod = (String) typeResult.getMap().get("classMethod");
        //动态调用bean
        Map<String, Object> paramMap = new HashMap<>();
        if (!StringUtils.isNull(param)) {
            paramMap = (Map) JSON.parse(param);
        }

        Object o = SpringContextHolder.getBean(classInstance);
        Method method = o.getClass().getMethod(classMethod, Map.class);
        Result resultObj = (Result) method.invoke(o, paramMap);
        return resultObj.getContent();

    }
}
